package com.hytc.mymall.pop;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Interpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hytc.mymall.R;

/**
 * Created by Administrator on 2016/12/1.
 */

public class NoticePop {
    private Activity activity;
    private InputMethodManager imm;
    private int left;
    private LinearLayout ll;
    public PopupWindow pop;
    private RelativeLayout rl_notice;
    private int top;

    public NoticePop(final Activity activity, final LinearLayout ll, final String text, final String text2, final String text3, final String text4, final int left, final int top) {
        this.activity = activity;
        this.ll = ll;
        this.left = left;
        this.top = top;
        final View inflate = LayoutInflater.from((Context)activity).inflate(R.layout.layout_notice, (ViewGroup)null);
        this.imm = (InputMethodManager)activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        this.pop = new PopupWindow(inflate, -1, -1, true);
        this.rl_notice = (RelativeLayout)inflate.findViewById(R.id.rl_notice);
        final TextView textView = (TextView)inflate.findViewById(R.id.tv_title);
        final TextView textView2 = (TextView)inflate.findViewById(R.id.tv_content);
        final TextView textView3 = (TextView)inflate.findViewById(R.id.tv_fromname);
        final TextView textView4 = (TextView)inflate.findViewById(R.id.tv_time);
        final TextView textView5 = (TextView)inflate.findViewById(R.id.tv_close);
        textView2.setMovementMethod(ScrollingMovementMethod.getInstance());
        if (!TextUtils.isEmpty((CharSequence)text)) {
            textView.setText((CharSequence)text);
        }
        if (!TextUtils.isEmpty((CharSequence)text2)) {
            textView2.setText((CharSequence)text2);
        }
        if (!TextUtils.isEmpty((CharSequence)text3)) {
            textView3.setText((CharSequence)text3);
        }
        if (!TextUtils.isEmpty((CharSequence)text4)) {
            textView4.setText((CharSequence)text4);
        }
        textView5.setOnClickListener(new View.OnClickListener() {
            public void onClick(final View view) {
                final AnimationSet animation = new AnimationSet(true);
                final ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.1f, 1.0f, 0.1f, 1, 0.5f, 1, 0.5f);
                scaleAnimation.setDuration(300L);
                scaleAnimation.setFillAfter(true);
                final TranslateAnimation translateAnimation = new TranslateAnimation(2, 0.0f, 2, 0.43f, 2, 0.0f, 2, 0.0f);
                translateAnimation.setDuration(300L);
                final TranslateAnimation translateAnimation2 = new TranslateAnimation(2, 0.0f, 2, 0.0f, 2, 0.0f, 2, -0.43f);
                translateAnimation2.setDuration(300L);
                animation.addAnimation((Animation)scaleAnimation);
                translateAnimation.setStartOffset(300L);
                translateAnimation2.setStartOffset(300L);
                translateAnimation.setInterpolator((Interpolator)new DecelerateInterpolator());
                translateAnimation2.setInterpolator((Interpolator)new DecelerateInterpolator());
                animation.addAnimation((Animation)translateAnimation);
                animation.addAnimation((Animation)translateAnimation2);
                NoticePop.this.rl_notice.setAnimation((Animation)animation);
                animation.setFillAfter(true);
//               animation.startNow();
                inflate.startAnimation(animation);
               animation.setAnimationListener(new Animation.AnimationListener() {
                   @Override
                   public void onAnimationStart(Animation animation) {

                   }

                   @Override
                   public void onAnimationEnd(Animation animation) {
                       NoticePop.this.pop.dismiss();
                   }

                   @Override
                   public void onAnimationRepeat(Animation animation) {

                   }
               });
            }
        });
        this.pop.setOnDismissListener(new PopupWindow.OnDismissListener() {
            public void onDismiss() {
            }
        });
    }

    public void Show(final Handler handler, final int n, final EditText softKeyboardShow) {
        this.setSoftKeyboardShow(softKeyboardShow);
        new Thread() {
            @Override
            public void run() {
                final Message obtain = Message.obtain();
                obtain.what = n;
                handler.sendMessageDelayed(obtain, 100L);
            }
        }.start();
    }

    public void setSoftKeyboardShow(final EditText editText) {
        if (this.imm.isActive()) {
            this.imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
        }
    }

    public void showPop() {
        AnimationUtils.loadAnimation((Context)this.activity, R.anim.pop_enter_right);
        this.pop.setBackgroundDrawable((Drawable)new ColorDrawable(0));
        this.pop.showAtLocation((View)this.ll, 17, 0, 0);
        this.pop.setAnimationStyle(R.anim.push_bottom_in);
        this.pop.setFocusable(true);
        this.pop.setOutsideTouchable(false);
        this.pop.setInputMethodMode(1);
        this.pop.setSoftInputMode(16);
        this.pop.update();
    }
}

